package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.LabType;

public class LabTypePresenter extends AbstractPresenter<LabType> {
	final String SINGLEITEM = "labType";
	final String MULTIITEM = "labTypes";
	
	public ArrayList<LabType> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public LabType tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public LabType createItemFromJSONObject(JSONObject jsonObject){
		LabType item = new LabType();
		try{
			item.setLtDescription(jsonObject.getString("ltdesc"));
			item.setLtID(jsonObject.getInt("ltid"));
			item.setLtPerBrew(jsonObject.getBoolean("ltPerBrew"));
			
			LabProcessPresenter presenter = new LabProcessPresenter();
			JSONObject jsonGroup = jsonObject.getJSONObject("labProcess");
			item.setLtProcess(presenter.createItemFromJSONObject(jsonGroup));
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(LabType item){
		try{
			JSONObject json = new JSONObject();
			json.put("ltdesc", item.getLtDescription());
			json.put("ltid", item.getLtID());
			json.put("ltPerBrew", item.isLtPerBrew());

			LabProcessPresenter presenter = new LabProcessPresenter();
			json.put("labProcess", presenter.createJSON(item.getLtProcess()));
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public LabType getEmptyEntity(){
		LabType entity = new LabType();
		return entity;
	}
}
